/*============================================================================
*  FILE: AirbagModel.c
*=============================================================================
* Copyright  2025 Shanghai Hongqu Automobile Technology Co., LTD. 
* All rights reserved.
* Confidential - Restricted Shanghai Hongqu Automobile Technology Co., LTD. 
* Do not disclose.
*-----------------------------------------------------------------------------
*
*  DESCRIPTION:
*  	 Telltale Airbag Model Logic
*
*
*===========================================================================*/

#include "AirbagModel.h"
#include "ssas_api.h"
#include "Common/DataStructureMacro.h"
#define LOG_TAG "TT"
#include "log_api.h"

//TODO 需求描述

//
#define AIRBAG_SELFCHECK_COUNT 6

static int AirbagGetEvent(AirbagWorkingData* workData)
{
    uint16_t rawData;

    //TODO get ignition state
    if (Com_ReceiveSignal(COM_SID_IGState, &rawData) != E_OK)
    {
        workData->event = EV_Unknown;
        // TODO log
        return E_NOT_OK;
    }

    if (workData->ignitionState == State_IgOff)
    {
        if (rawData == 1) 
            workData->event = EV_IgOn;
    }
    else if (workData->ignitionState == State_IgOn)
    {
        if (rawData == 0)
            workData->event = EV_IgOff;
        else 
        {
            // receive signal
            if (Com_ReceiveSignal(COM_SID_AirbgICIO, &rawData) != E_OK)
            {
                workData->event = EV_Unknown;
                // TODO log
                return E_NOT_OK;
            }

            if (rawData == 0)
                workData->event = EV_Off;
            else if (rawData == 1)
                workData->event = EV_On;
            else
            {
                workData->event = EV_Unknown;
                // TODO log
                return E_NOT_OK;
            }
        }
    }
    
    return E_OK;
}

static void AirbagStateTransition(AirbagWorkingData* workData)
{
    //selfcheck when ignition on
    if (workData->event == EV_IgOn)
    {
        workData->selfcheckCount = 0;        
        workData->ignitionState = State_IgOn;
        workData->currentState = State_Selfcheck;
    }
    //clear state when ignition off
    else if (workData->event == EV_IgOff)
    {
        workData->selfcheckCount = 0;        
        workData->ignitionState = State_IgOff;
        workData->currentState = State_Off;
    }
    //state machine
    else
    {
        switch (workData->currentState)
        {
            case State_Off:
            {
                if (workData->event == EV_On)
                    workData->currentState = State_On;
            }
            break;
            case State_Selfcheck:
            {
                workData->selfcheckCount++;

                if (workData->selfcheckCount >= AIRBAG_SELFCHECK_COUNT)
                {
                    workData->selfcheckCount = 0;
                    if (workData->event == EV_On)
                        workData->currentState = State_On;
                    else
                        workData->currentState = State_Off;
                }

            }
            break;
            case State_On:
            {
                if (workData->event == EV_Off)
                    workData->currentState = State_Off;
            }
            break;
            default:
            break;
        }
    }
    

}

static void init()
{
    AirbagWorkingDataInstance.currentState = State_Off;
    AirbagWorkingDataInstance.ignitionState = State_IgOff;
    AirbagWorkingDataInstance.event = EV_Off;
    AirbagWorkingDataInstance.selfcheckCount = 0;
    AirbagWorkingDataInstance.hasWidget = false;
}

static void run()
{
    // get event 
    AirbagGetEvent(&AirbagWorkingDataInstance);
    // state transition
    AirbagStateTransition(&AirbagWorkingDataInstance);
}

static void release()
{

}

AirbagModel g_AirbagModelInstance = {
    {
        init,
        run,
        release,
        MIN_PERIOD, // TODO
    },
};

// ----------------------------------View------------------------------
void refresh_tt10View(ItuElement* elememt, bool isUnconditional)
{
    //TODO
}

void reporting_self_validity_tt10View(ItuElement* elememt)
{
    AirbagWorkingDataInstance.hasWidget = (elememt && elememt->isVaild_) ? true : false;
}
